using System;
using System.Collections;
using System.Collections.Generic;
using GameBaseFramework.Extension;
using UnityEngine;

/// <summary>
/// 
/// </summary>
namespace GameLogic.AbilitySystem.Ability
{
    [Serializable]
    public class AbilityComponent
    {
        /// <summary>
        /// 父级组件
        /// </summary>
        public AbilitySystemComponent SelfAbilitySystemComponent;

        /// <summary>
        /// 持有的技能集合
        /// </summary>
        public List<AbilityBase> Abilities = new();

        /// <summary>
        /// Tag <=> 技能
        /// </summary>
        public Dictionary<string, LinkedList<AbilityBase>> TagAbilityDic = new();


        /// <summary>
        /// 赋予技能
        /// </summary>
        /// <returns></returns>
        public void GrantAbility(AbilityBase ability)
        {
            Abilities.Add(ability);

            foreach (var i in ability.Tags)
            {
                LinkedList<AbilityBase> linkedList;

                if (!TagAbilityDic.TryGetValue(i, out linkedList))
                {
                    linkedList = new();
                    TagAbilityDic.Add(i, linkedList);
                }

                linkedList.Enqueue(ability);
            }
        }

        ///// <summary>
        ///// 发动指定tag的技能 技能多的情况下最好用这边
        ///// </summary>
        ///// <param name="tag"></param>
        ///// <returns></returns>
        //public bool ActivateAbilityWithTag(string tag)
        //{
        //    if (!TagAbilityDic.TryGetValue(tag, out var linkedList))
        //    {
        //        return false;
        //    }
        //    else
        //    {
        //        linkedList.Last.Value.Activate(SelfAbilitySystemComponent);
        //        return true;
        //    }
        //}

        /// <summary>
        /// 发动指定tag的技能，遍历数组方式
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        public bool ActivateAbilityWithTag(string tag)
        {
            foreach (var item in Abilities)
            {
                if (item.ContainsTag(tag))
                {
                    item.Activate(SelfAbilitySystemComponent);
                    return true;
                }
            }

            return false;
        }
    }
}
